/**
 * 
 */
package info.niwota.start;

import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

/**
 * @author qiangli
 *
 */
public class WakeLockUtil {

	private static final String TAG = "WakeLockUtil";

	private WakeLockUtil() {
	}

	private static PowerManager.WakeLock mWakeLock;
	
	private static PowerManager pm;

	public static void acquireLock(Context ctx) {
		if (Debug.DEBUG) {
			Log.v(TAG, "Acquiring screen wake lock");
		}
		if (mWakeLock != null) {
			return;
		}
		
		if (pm == null) {
			pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
		}
		try {
			mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
					| PowerManager.ACQUIRE_CAUSES_WAKEUP
					| PowerManager.ON_AFTER_RELEASE, TAG);
			
			mWakeLock.acquire();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void releaseLock() {
		if (Debug.DEBUG) {
			Log.v(TAG, "Releasing wake lock");
		}
		//
		if (mWakeLock != null) {
			mWakeLock.release();
		}
		mWakeLock = null;
	}
	
}
